# -*- coding: utf-8 -*-
import os
import re
import time
import sys
import string
import xmlrpclib
import func.overlord.client as fc

#----------------------------------------------------------------------------#
# Name:        ModuleClass.py                                           #
# Purpose:     system Logic Module                                           #
# Author:      Liutiansi                                                     #
# Email:       liutiansi@gamil.com                                           #
# Created:     2008/11/03                                                    #
# Copyright:   (c) 2008                                                      #
#-----------------------------------------------------------------------------
#*func-0.25之后要求项：
#1、fc.Overlord不能不空；
#2、多主机名不能以";"号结束，应该截掉；
class ModuleClass():

    def Dict2str(self,Dict,serverlist,gettype=0):
        sharstr=""
        if gettype==1:
            return Dict
        for (host,details) in Dict.iteritems():
            
            for serveritem in serverlist:
                _serverlist=string.split(serveritem,'*')
                if _serverlist[0]==host:
                    _serverip=_serverlist[1]
                    break;
            sharstr+="主机：<b><font color=#ffffff>"+_serverip+"</font></b><br>"
            if details[0]==0:
                sharstr+="运行结果：<b><font color=#006699>成功</font></b><br>"
                sharstr+=details[1].replace('\n','<br>').encode('utf-8')
            else:
                sharstr+="运行结果：<b><font color=red>失败</font></b><br>"
                sharstr+=details[2].replace('\n','<br>').encode('utf-8')
            sharstr+="<br>"
        return sharstr
 
    def dorun(self,argvstr):
        for parstr in argvstr:
            if parstr.find(r'`')!=-1:
                return
        try:
            _serverliststr=""
            serverlist=string.split(argvstr[1],',')
            for serveritem in serverlist:
                _serverlist=string.split(serveritem,'*')
                _serverliststr+=str(_serverlist[0])+';'
            _serverliststr=_serverliststr[:len(_serverliststr)-1]
            client = fc.Overlord(_serverliststr)

            outformat=0
            try:
                commonname=str(argvstr[2])
                if commonname=="realserver":
                    source=str(argvstr[3])
                    goal="/home/LVS/realserver.sh"
                    hx="/bin/sh /home/LVS/realserver.sh start"

                elif commonname=="master" or commonname=="backup":
                    source=str(argvstr[3])
                    goal="/etc/keepalived/keepalived.conf"
                    hx="/etc/init.d/keepalived restart"

                        
                fb = file(source,"r").read()
                data = xmlrpclib.Binary(fb)
                client.copyfile.copyfile(goal,data)
                pinglist=client.command.run(hx)

            except Exception,e:
                pinglist=str(e)

        except Exception,e:
            print "error:"+str(e)

        print self.Dict2str(pinglist,serverlist,outformat)


if __name__ == '__main__':
    app = ModuleClass()
    app.dorun(sys.argv[1:])
